package blms.repository.infile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

import blms.repository.MatchupTableRepository;

public class InFileMatchupRepository implements MatchupTableRepository {
	
	private HashMap<Integer, String> tables = new HashMap<Integer, String>();

	public boolean containsTable(int scoreHigherRatedPlayer) {
		return this.tables.containsKey(scoreHigherRatedPlayer);
	}

	public void addTable(String pathOfTableFile, int scoreHigherRatedPlayer) {
		this.tables.put(scoreHigherRatedPlayer, pathOfTableFile);
	}

	public int getValueFor(int differenceBetweenStrongerAndWeaker, int scoreHigherRatedPlayerInt) {
		int smallestValue = 0, initialValue, finalValue;
	
		try {
			Scanner sc = new Scanner(new File(this.tables.get(scoreHigherRatedPlayerInt)));
			while (sc.hasNextInt()) {
				initialValue = sc.nextInt();
				finalValue = sc.nextInt();
				if (differenceBetweenStrongerAndWeaker >= initialValue && differenceBetweenStrongerAndWeaker <= finalValue) return sc.nextInt();
				smallestValue = sc.nextInt();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return smallestValue;
	}
}
